// var chineseLunar = require("chinese-lunar");
import chineseLunar from './calendar';
function Calendar() {
    this.years = null;
    this.month = null;
    this.day = null;
    this.weekDayArr = ['日','一','二','三','四','五','六'];
    this.initDate();
    this.renderWeek();
    this.render(this.years,this.month,this.day);
    this.slectDate();
    this.slectMonth();
};
/***初始化日期***/
Calendar.prototype.initDate = function () {
    var date = new Date();
    this.years = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.day = date.getDate();
};
Calendar.prototype.renderWeek = function(){
    let weekList = document.querySelector('.weekList ul');
    for (let i = 0; i < this.weekDayArr.length; i++) {
        var oli = document.createElement('li');
        oli.innerHTML = this.weekDayArr[i];
        if (i === 0 || i === this.weekDayArr.length - 1) {
            oli.className = 'hight'
        }
        weekList.append(oli);
    }
};
/***某个月有多少天***/
Calendar.prototype.getMonthDay=function(year, month) {
    let days = new Date(year, month, 0).getDate()
    return days
};
/***获取某月的日期***/
Calendar.prototype.getMonthDate= function(year, month, day) {
    var date = new Date(year, month - 1, day);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var weekDay = date.getDay();//星期几
    return {
        y,
        m,
        d,
        weekDay,
        manyDay: this.getMonthDay(y, m) //这个月总共多少天
    }
};
Calendar.prototype.render = function(year, month, day) {  
    let obj = this.getMonthDate(year, month, day);  
    let dateShow = document.querySelector('.dateSelect');
    let lunarDate = document.querySelector('.dateBoxTitleRight');
    dateShow.innerHTML = `${year}年&nbsp;&nbsp;&nbsp;&nbsp;${month < 10 ? '0' + month : month}月`
    /******************/
    let dateTody = new Date();
    let dateYears = dateTody.getFullYear();
    let dateMonth = dateTody.getMonth() + 1;
    let dateDay = dateTody.getDate();
    lunarDate.innerHTML = `农历${chineseLunar.solar2lunar(dateYears, dateMonth, dateDay).IMonthCn}${chineseLunar.solar2lunar(dateYears, dateMonth, dateDay).IDayCn}`
    /*****************/
    let weekDay = 1; //这个月1号是周几，默认值周一
    let dateArr = [];
    let lastMonth = this.getMonthDay(obj.y, obj.m - 1);//获取上个月最后一天的日期
    if (obj.d == 1) {
        weekDay = obj.weekDay;
    } else {
        weekDay = new Date(`${obj.y}-${obj.m}-${1}`).getDay();//获取这个月1号是周几
    };
    for (let i = weekDay; i > 0; i--) {
        let calM = chineseLunar.solar2lunar(obj.y, (obj.m  - 1), i + 1).IMonthCn;
        let cal = chineseLunar.solar2lunar(obj.y, (obj.m  - 1), (lastMonth - i) + 1).IDayCn;
        dateArr.push({
            isMonth: false,//是否是本月
            month: obj.m - 1,
            day: (lastMonth - i) + 1,
            IMonthCn: calM,
            IDayCn: cal,
        })
    };
    //构造日历页面的数组
    for (let i = 0; i < obj.manyDay; i++) {
        let calM = chineseLunar.solar2lunar(obj.y, (obj.m) ,i + 1).IMonthCn;
        let cal = chineseLunar.solar2lunar(obj.y, (obj.m) ,i + 1).IDayCn;
        dateArr.push({
            isMonth: true,//是否是本月
            month: obj.m,
            day: i + 1,
            isTody: i + 1 == obj.d ? true : false,
            IMonthCn:calM,
            IDayCn: cal
        })
    };
    let len = 42 - dateArr.length;
    //计算下个月日历
    for (let i = 0; i < len; i++) {
        let calM = chineseLunar.solar2lunar(obj.y, (obj.m+1), i + 1).IMonthCn;
        let cal = chineseLunar.solar2lunar(obj.y, (obj.m+1), i + 1).IDayCn;
      
        dateArr.push({
            isMonth: false,//是否是本月
            month: obj.m + 1,
            day: i + 1,
            IMonthCn: calM,
            IDayCn: cal,
        })
    };
    let oul = document.querySelector('.dayList ul');
    oul.innerHTML='';
    for (let i = 0; i < dateArr.length; i++) {
        var oli = document.createElement('li');
        oli.setAttribute('data-lunar', `${JSON.stringify(dateArr[i])}`);
        var div = document.createElement('div');
        var div1 = document.createElement('div');
        var div2 = document.createElement('div');
        if (dateYears === year && dateMonth === month && dateDay === dateArr[i].day) {
            div.className = 'isToady';
            div1.style.color = "#fff";
            div2.style.color = "#fff";
        }
        div1.innerHTML = dateArr[i].day;
        div2.innerHTML = dateArr[i].IDayCn;
        if (dateArr[i].isMonth) {
            div1.className = 'active';
            div2.className = 'active';
        }
        oli.append(div);
        div.append(div1, div2)
        oul.append(oli);
    }
}
//月份选择
Calendar.prototype.slectMonth = function(){
    var self = this;
    $('.monthAdd').on('click',function(){
        if(self.month===12){
            self.years++;
            self.month=1;
        }else{
            self.month++;
        };
        self.render(self.years,self.month,self.day);

    })
    $('.montSubtract').on('click',function(){
        if(self.month===1){
            self.years--;
            self.month=1;
        }else{
            self.month--;
        };
        
        self.render(self.years,self.month,self.day);
    })
}
//日子选择
Calendar.prototype.slectDate=function(){
    $('.dayList ul').on('click','li',function(){
        let lunar = JSON.parse($(this).attr('data-lunar'));
        if(lunar.isMonth){
            $('.dateBoxTitleRight').html(`农历${lunar.IMonthCn}${lunar.IDayCn.indexOf('月')!=-1?'初一':lunar.IDayCn}`);
            $(this).children('div').css('border','1px solid #999999');
            $(this).siblings().children('div').css('border','none');
        };
    }) 
};
export default Calendar;